<?
// 参数表: 产品id
// params: product_id 

// history 浏览历史
function get_history(){
	$tem=array();
	if(isset($_COOKIE['history'])&&!empty($_COOKIE['history'])){
		$cookie_cart=$_COOKIE['history'];
		$tem=explode('&',$cookie_cart);
	}
	return $tem;
}

function add_to_history($pid){
	$sameSkuExist=false;
	$res=array();
	if(isset($_COOKIE['history'])&&!empty($_COOKIE['history'])){
		$cookie_cart=$_COOKIE['history'];
		$history=explode('&',$cookie_cart);
		foreach($history as $val){
			if($pid==$val){
				$sameSkuExist=true;
			}
			$res[]=$val;
		}
	}
	if($pid!=0 && !$sameSkuExist){
		$res[]=$pid;
	}
	write_to_history($res);
	return count($res);
}

function write_to_history($history){
	$k=0;
	$cart_str='';
	foreach($history as $val){
		if($k)$cart_str.='&';
		$cart_str.=$val;
		$k++;
	}
	setcookie("history", $cart_str,time()+3600*24*30);
}

function history_clear(){
	setcookie("history", '', time()-3600);
	return 1;
}

// end of file